﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Text;
using System.Text.RegularExpressions;
using WCrawlerLib;
using HtmlAgilityPack;

namespace WCrawler.TestPages
{
    public partial class frmTestHostManager : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string username = HttpUtility.UrlEncode(txtUsername.Text);
            string password = HttpUtility.UrlEncode(txtPassword.Text);
            string folder = HttpUtility.UrlEncode(txtFolderName.Text);
            BrowserSession_FileHost br = new BrowserSession_FileHost();
            //Open host link
            //br.Get("http://fileserve.com/");
            string param = string.Format("loginUserName={0}&loginUserPassword={1}&autoLogin=on&ppp=102&loginFormSubmit=Login",username,password);
            br.PostParams = param;
            br.Post("http://fileserve.com/login.php");
            br.PostParams = string.Format("newFolderName={0}&createFolderFormSubmit=Create+folder&folderId=&createFolderDest=2418005",folder);
            br.Post("http://fileserve.com/file-manager.php");
        }

        protected void btnFileFactory_Click(object sender, EventArgs e)
        {
            string email = HttpUtility.UrlEncode(txtEmail.Text);
            string password = HttpUtility.UrlEncode(txtPassword.Text);
            string folder = HttpUtility.UrlEncode(txtFolderName.Text);
            BrowserSession_FileHost br = new BrowserSession_FileHost();
            //Open host link
            string param = string.Format("redirect=%2F&email={0}&password={1}", email, password);
            br.PostParams = param;
            br.Post("http://filefactory.com/member/login.php");

            br.Get("http://filefactory.com/manager/");

            //Add NewFolder in Movie Folder
            br.PostParams = string.Format("m=addFolder&p=209b8cf270dd296b"); //p is parent folder
            HtmlDocument doc = br.Post("http://filefactory.com/manager/set.php");

            //Get created folder id
            //Template: {status:"ok",data:{attributes:{id:"437ef5169e192c35",mdata:"{}"},data:"New folder"}}
            Regex r = new Regex("id:\"(?<FolderId>\\w+)\"");
            Match m = r.Match(doc.DocumentNode.InnerHtml);
            string folderId = "";
            if (m.Success)
            {
                folderId = m.Result("${FolderId}");
                //Rename created NewFolder
                br.PostParams = string.Format("m=editFolder&f={0}&n={1}", folderId, folder);
                br.Post("http://filefactory.com/manager/set.php");
            }
        }

        /// <summary>
        /// Test Post with depositfiles
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDepositfiles_Click(object sender, EventArgs e)
        {
            string username = HttpUtility.UrlEncode(txtUsername.Text);
            string password = HttpUtility.UrlEncode(txtPassword.Text);
            string folder = HttpUtility.UrlEncode(txtFolderName.Text);
            BrowserSession_FileHost br = new BrowserSession_FileHost();

            //Open host link
            br.PostParams = string.Format("go=1&login={0}&password={1}", username, password);
            br.Post("http://depositfiles.com/login.php?return=%2F");

            //Add folder
            br.PostParams = string.Format("action=add_folder&name={0}", folder);
            br.Post("http://depositfiles.com/gold/files_list.php?format=json");

        }

        protected void btnFileSonic_Click(object sender, EventArgs e)
        {
            string url = "";
            string username = HttpUtility.UrlEncode(txtUsername.Text);
            string password = HttpUtility.UrlEncode(txtPassword.Text);
            string folder = HttpUtility.UrlEncode(txtFolderName.Text);

            string parentFolderId = "D605169"; //D605169 is Movie Folder

            BrowserSession_FileHost br = new BrowserSession_FileHost();

            //Open host link
            br.PostParams = string.Format("email={0}&redirect=%2F&password={1}", username, password);
            br.Post("http://www.filesonic.vn/user/login");

            br.PostParams = "folderId=D605169";
            url = string.Format("http://www.filesonic.vn/filesystem/browse/{0}/1/name/asc/thisFolder/?partial=fileList", parentFolderId);
            br.Post(url);

            url = string.Format("http://www.filesonic.vn/filesystem/browse/{0}?partial=folderTree&orderFoldersBy=name&orderFoldersDirection=asc",parentFolderId);
            br.Get(url);

            url = string.Format("http://www.filesonic.vn/filesystem/create/{0}", parentFolderId);
            br.Get(url); 

            //Add folder
            br.PostParams = string.Format("redirect=%2Ffilesystem%2Fbrowse&id=&name={0}&description=&parent_folder_id={1}&is_public=0&can_access_parent=0", folder, parentFolderId);
            url = string.Format("http://www.filesonic.vn/filesystem/create/{0}", parentFolderId);
            br.Post(url); 
        }
    }
}